// constructor
var toolkit = {
    "tr909" :
    [
        ["Bass Drum", "bassdm/BT0A0A7.WAV"],
        ["Hand Clap", "handclap/HANDCLP1.WAV"],
        ["Closed HH", "closedhh/HHCD0.WAV"],
        ["Open HH", "openhh/HHOD0.WAV"],        
        ["Snare Drum", "snaredm/ST7T3S7.WAV"],
        ["Hi Tom", "hitomdm/HT0D3.WAV"]
    ],
    "nuskool" :
    [
        ["Bass Drum", "nuskool/bdrum.wav"],
        [">empty<", ""],
        ["Closed HH", "nuskool/chh.wav"],
        ["Ghost Snare", "nuskool/gsd.wav"],
        ["Snare Drum", "nuskool/sd.wav"]
    ],
    "oldskool" :
    [
        ["Bass Drum", "bdrum.wav"],
        ["Closed HH low", "chh-silent.wav"],
        ["Closed HH", "chh.wav"],
        ["Ghost Snare", "gsnare.wav"],
        ["Snare Drum", "snare.wav"]
    ]    
};
// constructor

function DrumMachine () {
    this.loadToolkit('tr909');
}

DrumMachine.prototype.loadToolkit = function (name) {
    var samples = toolkit[name];
    this.numDrums = samples.length;
    generateSequencer(this.numDrums);
    this.drums = new Array();
    this.muted = new Array();
    for (var i=0; i<this.numDrums; i++) {
        var elem = document.querySelectorAll('.instrument')[i];
        if (elem) {
            elem.innerHTML = samples[i][0];
        }
        this.drums[i] = new Audio(samples[i][1]);
        
        registerAudioCallback(this.drums[i]);
    }
}

DrumMachine.prototype.play = function (inst, note) {
    var index = inst%this.numDrums;
    if (note != 0) {
        if (!song.isMuted(inst)) {
            this.drums[index].play();
        }
    }
}

DrumMachine.prototype.getNumDrums = function () {
    return this.numDrums;
}

var player = null;
var dm = null;
var prog = null;
var song = null;
function init() {
    loadToolkits();    
    song = new Song();
    dm = new DrumMachine();
    prog = new DummyProgramator();
    player = new Player(dm, prog, 140, 0);
    player.play();
    document.querySelector("body").onkeydown = function (e) {
        if (e.which >= 48 && e.which <=57) {            
            loadSong(e.which-48);
        }
    };    
}

